package org.dtrd.modules.service.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.service.entity.bean.ServiceCategoryInfo;
import org.dtrd.modules.service.service.IDtrdEntRdServiceCategoryService;
import org.dtrd.modules.service.service.IDtrdEntRdServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 服务套餐分类信息 前端控制器
 * </p>
 *
 * @author QR
 * @since 2021-11-19 18:52:54
 */
@Api(tags = "服务套餐")
@RestController
@RequestMapping("/107/03")
public class ServiceCategoryController extends BaseController {

    @Autowired
    private IDtrdEntRdServiceCategoryService serviceCategoryService;
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IDtrdEntRdServiceService serviceService;

    @ApiOperation(value = "获取服务套餐分类列表",notes = "根据机构编码获取服务套餐分类列表",
            response = ServiceCategoryInfo.class)
    @GetMapping("/1070306")
    @AutoLog(value="获取服务套餐分类列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getServiceCategoryList(@PathParam(value = "orgCode") String orgCode) {
//        List<ServiceCategoryInfo> serviceCategoryInfoList = serviceCategoryService.getServiceCategoryList();
        List<ServiceCategoryInfo> serviceCategoryInfoList = serviceCategoryService.getServiceCategoryList(orgCode);
        return Result.OK(serviceCategoryInfoList);
    }


    @ApiOperation("编辑服务套餐分类内容")
    @PostMapping("/1070307")
    @AutoLog(value="编辑服务套餐分类内容", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateServiceCategoryInfo(@RequestBody @Validated ServiceCategoryInfo dto) {
        Integer dataId = dto.getDataId();
        String categoryName = dto.getCategoryName();
        // 只校验分类的名字
        if (StrUtil.isBlank(categoryName)) {
            return error(STATE_PARAMS_MISSING);
        }
        if (serviceCategoryService.hasSameServiceCategoryName(dataId, categoryName, dto.getOrgCode())) {
            return Result.error("不能有同名的分类");
        }
        boolean result = serviceCategoryService.saveOrUpdateServiceCategoryInfo(dto);
        String logContent = String.format("编辑服务套餐分类内容, 编辑内容：%s", dto);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }


    @ApiOperation(value = "删除服务套餐分类")
    @DeleteMapping("/1070308")
    public Result<?> removeServiceCategoryInfo(@RequestParam Integer categoryId) {
        if (serviceService.hasCategoryService(categoryId)) {
            return Result.error("该分类已经关联服务不能删除");
        }
        boolean result = serviceCategoryService.removeServiceCategoryInfo(categoryId);
        String logContent = String.format("删除服务套餐分类, 删除分类id：%d", categoryId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
